package com.google.denta.service;

import java.util.List;

import org.apache.commons.configuration.Configuration;

import com.google.denta.dao.UserDao;
import com.google.denta.entity.Group;
import com.google.denta.entity.PaginatorForm;
import com.google.denta.entity.Role;
import com.google.denta.entity.User;

/**
 * User service implementation.
 * 
 * @author Kiril_Akshonau
 */
public class UserServiceImpl implements UserService {

    /** Dao. */
    private UserDao userDao;

    /** Approver dao. */

    /** configuration. */
    private Configuration conf;

    /**
     * Setter for userDao.
     * 
     * @param dao - Dao
     */
    public void setUserDao(final UserDao dao) {
        this.userDao = dao;
    }

    /**
     * @param conf the conf to set
     */
    public void setConf(Configuration conf) {
        this.conf = conf;
    }

    /** {@inheritDoc} */
    public List<User> getUsers() {
        return userDao.findAll();
    }

    /** {@inheritDoc} */
    public User getUser(final Long id) {
        return userDao.read(id);
    }

    /** {@inheritDoc} */
    public User getUserByName(final String name) {
        return userDao.findByName(name);
    }

    /** {@inheritDoc} */
    public void saveUser(final User user) {
        if (user.getId() == null) {
            final Long id = userDao.create(user);
            user.setId(id);
        } else {
            userDao.update(user);
        }
        saveUserRoles(user);
        saveUserGroups(user);
    }

    /** {@inheritDoc} */
    public void saveUserRoles(final User user) {
        userDao.deleteUserRoles(user.getId());
        if (user.getRoles() != null) {
            for (Role role : user.getRoles()) {
                userDao.createUserRole(user.getId(), role.getId());
            }
        }
    }

    /** {@inheritDoc} */
    public void saveUserGroups(final User user) {
        userDao.deleteUserGroups(user.getId());
        if (user.getGroups() != null) {
            for (Group group : user.getGroups()) {
                userDao.createUserGroup(user.getId(), group.getId());
            }
        }
    }

    /** {@inheritDoc} */
    public void deleteUser(final Long id) {
        userDao.deleteUserGroups(id);
        userDao.deleteUserRoles(id);
        userDao.delete(id);
    }

    /** {@inheritDoc} */
    public void deleteUsers(final Long[] ids) {
        if (ids != null && ids.length > 0) {
            for (Long id : ids) {
                deleteUser(id);
            }
        }
    }

    /** {@inheritDoc} */
    public User getByPassword(final Long id, final String password) {
        return userDao.getByPassword(id, password);
    }

    /** {@inheritDoc} */
    public void update(final User user) {
        userDao.update(user);
    }

    /** {@inheritDoc} */
    public void updatePassword(final User user) {
        userDao.updatePassword(user);
    }

    /** {@inheritDoc} */
    public User getByPersone(Long id) {
        return userDao.getByPersone(id);
    }

    /** {@inheritDoc} */
    public List<User> paginatorFind(PaginatorForm form) {
        Long count = conf.getLong("employees.on.page");
        if (!form.getCountOnPage().equals(count)) {
            form.setCurrentPage(1L);
            form.setCountOnPage(count);
            form.setStartFrom(0L);
        }
        return userDao.paginatorFind(form);
    }

    /** {@inheritDoc} */
    public Long getUsersCount() {
        return userDao.getUsersCount();
    }
}
